﻿var grid, cbProgress;


//初始化表格
function InitGrid() {
    var AreaID = $('#AreaID').combobox('getValue');
    var PortID = $('#PortID').combobox('getValue');
    if (AreaID == null || AreaID == "") {
        AreaID = "0";
    }
    if (PortID == null || PortID == "") {
        PortID = "0";
    }
    $('#AreaID').combogrid("setValue", AreaID);
    $('#PortID').combogrid("setValue", PortID);
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/AreaPort/Query',
        title: '货位列表',
        queryParams: {
            Area: AreaID,
            SendPort: PortID
        },
        height: 530,
        fitColumns: false,
        rownumbers: true,
        singleSelect: true,
        idField: 'AREAPORTID',
        columns: [[
                    { field: 'AREAPORTID', title: '主键ID', hidden: true },
                    { field: 'AREAID', title: '区域编码', align: 'center', sortable: true },
                    { field: 'AreaName', title: '区域名称', align: 'center', sortable: true },
                    { field: 'SENDPORTID', title: '发货口编码', align: 'center', sortable: true },
                    { field: 'SendPortName', title: '发货口', align: 'center', sortable: true },
                    { field: 'CREATER', title: '创建人', align: 'center', sortable: true },
                    { field: 'CREATETIME', title: '创建时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'MODITOR', title: '修改人', align: 'center', sortable: true },
                    { field: 'MODITIME', title: '修改时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'REMARK', title: '备注', align: 'center', width:100 , sortable: true }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}
function formatstatus(val) {
    if (val != null) {
        if (val == 0) {
            return "失效";
        }
        if (val == 1) {
            return "生效";
        }
    }
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}

//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增货位资料信息');
    $("#hiddenAREAPORTID").val("");
    $('#AreaID1').combogrid("setValue", "");
    $('#PortID1').combogrid("setValue", "");
    $('#txtRemark').val("");
    url = '/AreaPort/Saves';

}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改货位资料信息');
    $("#hiddenAREAPORTID").val(rows[0].AREAPORTID);
    $('#AreaID1').combogrid("setValue", rows[0].AREAID);
    $('#PortID1').combogrid("setValue", rows[0].SENDPORTID);
    $('#txtRemark').val(rows[0].REMARK);
    url = '/AreaPort/Saves';

}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].AREAPORTID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#areaportID').form('submit', {
                url: '/AreaPort/Delete' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function save() {
    var AREAPORTID = $.trim($("#hiddenAREAPORTID").attr("value"));
    var AreaID = $('#AreaID1').combobox('getValue');
    var SendPortID = $('#PortID1').combobox('getValue');
    var Remark = $('#txtRemark').attr("value");

    if (AreaID == null || AreaID == "") {
        $.messager.alert('提示', '请选择区域', 'warning');
        $("#AreaID1").focus();
        return false;
    }
    if (SendPortID == null || SendPortID == "")
    {
        $.messager.alert('提示', '请选择发货口', 'warning');
        $("#PortID1").focus();
        return false;
    }
    if (Remark.length > 200)
    {
        $.messager.alert('提示', '备注请控制在200字以内', 'warning');
        $("#txtRemark").focus();
        return false;
    }
 


    $('#areaportID').form('submit', {
        url: url + '?AreaID=' + AREAPORTID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}